class AbstractProductsController < ApplicationController
  # GET /abstract_products
  # GET /abstract_products.xml
  #  def index
  #    @abstract_products = AbstractProduct.find(:all)
  #
  #    respond_to do |format|
  #      format.html # index.html.erb
  #      format.xml  { render :xml => @abstract_products }
  #    end
  #  end
  #
  #  # GET /abstract_products/1
  #  # GET /abstract_products/1.xml
  #  def show
  #    @abstract_product = AbstractProduct.find(params[:id])
  #
  #    respond_to do |format|
  #      format.html # show.html.erb
  #      format.xml  { render :xml => @abstract_product }
  #    end
  #  end

  # GET /abstract_products/new
  # GET /abstract_products/new.xml
  def new
    @abstract_product = AbstractProduct.new
    @products = Product.find_all_by_brand_id_and_category_id(params[:brand_id],params[:category_id])
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @abstract_product }
    end
  end

  # GET /abstract_products/1/edit
  def edit
    @abstract_product = AbstractProduct.find(params[:id])
    product = @abstract_product.products.first
    @products = Product.find_all_by_brand_id_and_category_id(product.brand_id,product.category_id)

  end

  # POST /abstract_products
  # POST /abstract_products.xml
  def create
    @abstract_product = AbstractProduct.new(params[:abstract_product])

    for item in params[:product].values
      #chech_box没有选中的时候，提交的值为"0"
      @abstract_product.products << Product.find(item.values[0]) if item.values[0] !="0"
    end

    respond_to do |format|
      if @abstract_product.save
        flash[:notice] = "母商品创建成功."
        format.html { redirect_to("/categories/#{params[:category_id]}") }
        format.xml  { render :xml => @abstract_product, :status => :created, :location => @abstract_product }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @abstract_product.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /abstract_products/1
  # PUT /abstract_products/1.xml
  def update
    @abstract_product = AbstractProduct.find(params[:id])
    back_path = "/categories/#{@abstract_product.products.first.category_id}"

    @abstract_product.products.clear
    for item in params[:product].values
      #chech_box没有选中的时候，提交的值为"0"
      @abstract_product.products << Product.find(item.values[0]) if item.values[0] !="0"
    end
    @abstract_product.save

    respond_to do |format|
      if @abstract_product.update_attributes(params[:abstract_product])
        flash[:notice] = '母商品更新成功.'
        format.html { redirect_to(back_path) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @abstract_product.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /abstract_products/1
  # DELETE /abstract_products/1.xml
  def destroy
    @abstract_product = AbstractProduct.find(params[:id])
    category_id = @abstract_product.products.first.category_id
    @abstract_product.destroy

    respond_to do |format|
      format.html { redirect_to("/categories/#{category_id}") }
      format.xml  { head :ok }
    end
  end
end
